<template>
    <el-form :model="formData" ref="formref" :rules="rules" label-width="80px" :inline="false" size="normal">
        <el-form-item label="类目名称" prop="cname">
            <el-input v-model="formData.cname"></el-input>
        </el-form-item>
        <el-form-item label="类目图片" prop="cimg">
            <el-input v-model="formData.cimg"></el-input>
            <TuKu v-model="formData.cimg"></TuKu>
        </el-form-item>
        <el-form-item label="类目排序" prop="seq">
            <!-- <el-input v-model="formData.seq"></el-input> -->
            <el-input-number v-model="formData.seq" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="父目录ID" prop="parentId">
            <!-- <el-input v-model="formData.parentId"></el-input> -->
            <el-tree-select v-model="formData.parentId" :data="CategoryTreeData" check-strictly
                :render-after-expand="false" style="width: 240px" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit(formref)">提交</el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
    </el-form>




</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import TuKu from '../../components/TuKu.vue';
import { CategoryApi } from '../../api';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';
const router = useRouter();
const formref = ref(null);
const formData = reactive({
    cname: '',
    cimg: '',
    seq: '',
    parentId: ''
})
const rules = reactive({
    cname: [
        { required: true, message: '请输入类目的名称', trigger: 'blur' },
        { min: 2, max: 10, message: '类目名称字符数在2 到 10 字符', trigger: 'blur' },
    ],
})
//树形选择结构
const CategoryTreeData = ref([
    {
        value: 0,
        label: '一级目录',
    }
])

onMounted(() => {
    let henan = ["河南烩面", "洛阳驴肉汤", "道口烧鸡", "DL月饼", "开封桶子鸡"]
    const eatchat = henan.reduce((duzi, currentValue) => {
        if (currentValue == "洛阳驴肉汤") { duzi.push(currentValue) }
        if (currentValue == "DL月饼") { duzi.push(currentValue) }
        return duzi;
    }, [])

    console.log(eatchat);
    CallCategoryApi()
})

const CallCategoryApi = () => {
    CategoryApi.select.call().then(res => {
        res.forEach(item => {
            item.value = item.id
            item.label = item.cname
        })
        console.log(res);
        const categoryTreeList = res.reduce((hezi, currentValue) => {
            currentValue.children = res.filter(item => {
                return item.parentId == currentValue.id
            })
            if (currentValue.parentId == 0) {
                hezi.push(currentValue)
            }
            return hezi;
        }, [])
        console.log(categoryTreeList);
        CategoryTreeData.value = categoryTreeList
    })
}

const formSubmit = () => {
    CategoryApi.insert.call({
        cname: formData.cname,
        cimg: formData.cimg,
        seq: formData.seq,
        parentId: formData.parentId
    }).then(res => {
        console.log(res);
        if (res == 1) {
            ElMessage.success('添加成功')
            router.push('/category/list')
            // router.push({name:'categoryList'})
            CallCategoryApi()
        }
    })
}

const onSubmit = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')
            formSubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style scoped></style>